package com.etc.library.configuration;

import com.etc.library.service.RecordService;
import com.etc.library.util.EmailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class ScheduleMailTask {

    @Autowired
    private RecordService recordService;

    @Autowired
    private EmailUtil util;

    @Value("${warn.type}")
    private String warnType;

    @Scheduled(cron = "0 0 0 * * ?")
    public void autoSendMail(){
        List<Map<String, Object>> list = recordService.sendWarningMail(warnType);
        list.forEach(map->{
            String subject = String.format("关于%s图书归还提醒",map.get("b_name"));
            String content = String.format("尊敬的用户：你好，您借阅的《%s》图书将于 %s 天后(%s) 到期，请及时归还。",map.get("b_name"),map.get("sub_num"),map.get("end_time"));
            String to = map.get("email").toString();
            util.send(subject,content,to);
        });
    }
}
